﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ImageRecognition2
{
    /// <summary>
    /// Directory Acquiring
    /// </summary>
    public partial class FormDirectoryAcquiring : Form
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        string sPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Bluetooth-Exchange-Ordner";
        /// <summary>
        /// Initializes a new instance of the <see cref="FormDirectoryAcquiring"/> class.
        /// </summary>
        
        public FormDirectoryAcquiring()            
        {
            InitializeComponent();
            watcher.Path = sPath;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FormDirectoryAcquiring"/> class.
        /// </summary>
        /// <param name="sPath">The s path.</param>
        public FormDirectoryAcquiring(string _sPath)
            : base()
        {
            InitializeComponent();

            if (!string.IsNullOrEmpty(sPath))
            {
                watcher.Path = _sPath;
            }
            watcher.EnableRaisingEvents = true;
            watcher.IncludeSubdirectories = true;
            watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.Attributes;
            watcher.Changed += new FileSystemEventHandler(watcher_Changed);
            watcher.Created += new FileSystemEventHandler(watcher_Changed);
            watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
            this.pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;

            string sText = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        }

        /// <summary>
        /// Gets the image.
        /// </summary>
        /// <value>The image.</value>
        public Image Image
        {
            get { return this.pictureBox1.BackgroundImage; }
        }

        /// <summary>
        /// Handles the Renamed event of the watcher control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.IO.RenamedEventArgs"/> instance containing the event data.</param>
        void watcher_Renamed(object sender, RenamedEventArgs e)
        {
            MessageBox.Show(e.FullPath);
        }

        /// <summary>
        /// Handles the Changed event of the watcher control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.IO.FileSystemEventArgs"/> instance containing the event data.</param>
        void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            string sExtension = Path.GetExtension(e.FullPath);
            if (sExtension == ".jpg")
            {
                try
                {
                    //if (this.pictureBox1.BackgroundImage != null) this.pictureBox1.BackgroundImage.Dispose();
                    this.pictureBox1.BackgroundImage = Image.FromFile(e.FullPath);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the cbFolder control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            if (fd.ShowDialog() != DialogResult.OK) return;
            watcher.Path = fd.SelectedPath;
        }

        /// <summary>
        /// Handles the Click event of the cbOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the cbCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
